package com.pug.zixun.config.handler;

import com.pug.zixun.commons.enums.AdminErrorResultEnum;
import com.pug.zixun.commons.ex.ErrorHandler;
import com.pug.zixun.commons.ex.PugBusinessException;
import com.pug.zixun.commons.ex.PugOrderException;
import com.pug.zixun.commons.ex.PugValidatorException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 统一异常处理
 * @Author: WengYu
 * @CreateTime: 2022/05/18 23:15
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 统一异常返回
     *
     * @param e
     * @param request
     * @return
     */
    @ExceptionHandler(Throwable.class)
    public ErrorHandler makeException(Throwable e, HttpServletRequest request) {
        e.printStackTrace();
        return ErrorHandler.fail(AdminErrorResultEnum.SERVER_ERROR.getCode(),AdminErrorResultEnum.SERVER_ERROR.getMessage(), e);
    }

    /**
     * 业务异常
     *
     * @param e
     * @param request
     * @return
     */
    @ExceptionHandler(PugBusinessException.class)
    public ErrorHandler businessException(PugBusinessException e, HttpServletRequest request) {
        e.printStackTrace();
        return ErrorHandler.fail(e.getCode(),e.getMessage(), e);
    }

    /**
     * 订单异常
     *
     * @param e
     * @param request
     * @return
     */
    @ExceptionHandler(PugOrderException.class)
    public ErrorHandler orderException(PugOrderException e, HttpServletRequest request) {
        e.printStackTrace();
        return ErrorHandler.fail(e.getCode(),e.getMessage(), e);
    }

    /**
     * 校验异常
     *
     * @param e
     * @param request
     * @return
     */
    @ExceptionHandler(PugValidatorException.class)
    public ErrorHandler validatorException(PugValidatorException e, HttpServletRequest request) {
        e.printStackTrace();
        return ErrorHandler.fail(e.getCode(),e.getMessage(), e);
    }



}
